package les.projeto;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

public class AddActivityView extends Activity{
	
	public Calendar date;
	public int today;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.addactivity);
	    date = Calendar.getInstance();
	    onNewIntent(getIntent());	    
	
	    getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
	    
	    final TimePicker timePicker = (TimePicker)findViewById(R.id.timePicker1);
	    timePicker.setIs24HourView(true);
        
	    final DatePicker picker = (DatePicker)findViewById(R.id.datePicker1);
	    picker.setCalendarViewShown(false);
	    today = picker.getDayOfMonth();
	    picker.init(date.get(Calendar.YEAR), date.get(Calendar.MONTH), today, null);
	    
	    Button save = (Button)findViewById(R.id.buttonSave);
	    save.setOnClickListener(new OnClickListener() {
	        
			
			@Override
			public void onClick(View v) {
				EditText name = (EditText)findViewById(R.id.editTextName);
				String nameText = name.getText().toString();
				if (nameText.isEmpty() || nameText.trim().equals("Enter Here")){
					AlertDialog.Builder builder = new AlertDialog.Builder(AddActivityView.this);
					builder.setMessage("Please, enter a name")
					       .setCancelable(false)
					     
					       .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					           }
					       });
					builder.show();
				}else {
					EditText description = (EditText)findViewById(R.id.editTextDescription);
					String descriptionText = description.getText().toString();
					if (descriptionText.trim().equals("Enter Here"))
						descriptionText = "Any";
					int day = picker.getDayOfMonth();
					int month = picker.getMonth();
					int year = picker.getYear();
					
					int hour = timePicker.getCurrentHour();
					int minute = timePicker.getCurrentMinute();
					
					String hourPicked = hour + ":" + minute;
					
					String datePicked = day + "-" + month + "-" + year;

					insertBD(datePicked, hourPicked, nameText, descriptionText);
					AlertDialog.Builder builder = new AlertDialog.Builder(AddActivityView.this);
					builder.setMessage("Task Save")
						       .setCancelable(false)	
					  		
						       .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
						           public void onClick(DialogInterface dialog, int id) {
						                dialog.cancel();
						                back();
						           }
						       });
					builder.show();
			
				}
				
			}
		});
	    
	}
	
	protected void insertBD(String datePicker, String hourPicked, String nameText, String descriptionText) {
		BD bd = new BD();
		bd.insertActivity(this, datePicker, hourPicked, nameText, descriptionText);		
	}

	public void onNewIntent(Intent intent) {
		String dateText = intent.getStringExtra("date");
		String[] dateArr = dateText.split("-"); // date format is yyyy-mm-dd
		date.set(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2]));
	}
	
	@Override
	public void onBackPressed() {
        back();	        		 
        return;
	}

	private void back() {
		int month = date.get(Calendar.MONTH);
        int year = date.get(Calendar.YEAR);
        
	    Intent trocatela = new Intent(AddActivityView.this, CalendarView.class);
		trocatela.putExtra("date", year+"-"+month+"-"+today);

        AddActivityView.this.startActivity(trocatela);
        AddActivityView.this.finish();
	}
}
